package 算法.练习1;

/**
 * @author:谢君臣
 * @Date:2021/7/20-10:49
 * @version:1.0
 * @function:
 */
public class NC105 {
    public static void main(String[] args) {
        System.out.println(search(new int[]{1,1,2,3,7,7,7,9,9,10}, 11));
    }
    public static int search (int[] nums, int target) {
        // write code here
        if(nums.length<1||nums[nums.length-1]<target)
            return -1;
        else
            return dichotomySearch(nums,0,nums.length-1,target);
    }
    public static int dichotomySearch(int[] nums,int l,int h, int target){
        if(l<=h){
            if(nums[l]==target)
                return l;
            else if (nums[l]>target)
                return -1;
            else{
                int mid = (h-l)/2 + l;
                if(nums[mid]>=target)
                    h = mid;
                else
                    l = mid+1;
                return dichotomySearch(nums,l,h,target);
            }
        }else{
            return -1;
        }
    }
}
